home *** CD-ROM | disk | FTP | other *** search
/ Aminet 48 / Aminet 48 (2002)(GTI - Schatztruhe)[!][Apr 2002].iso / Aminet / text / edit / vim60rt.lha / Vim / vim60 / doc / autocmd.txt next >
Encoding:
Text File  |  2001-09-26  |  36.6 KB  |  884 lines

  1. *autocmd.txt*   For Vim version 6.0.  Last change: 2001 Sep 03
  2.  
  3.  
  4.           VIM REFERENCE MANUAL    by Bram Moolenaar
  5.  
  6.  
  7. Automatic commands                    *autocommand*
  8.  
  9. For a basic explanation, see section |40.3| in the user manual.
  10.  
  11. 1.  Introduction        |autocmd-intro|
  12. 2.  Defining autocommands    |autocmd-define|
  13. 3.  Removing autocommands    |autocmd-remove|
  14. 4.  Listing autocommands    |autocmd-list|
  15. 5.  Events            |autocmd-events|
  16. 6.  Patterns            |autocmd-patterns|
  17. 7.  Groups            |autocmd-groups|
  18. 8.  Executing autocommands    |autocmd-execute|
  19. 9.  Using autocommands        |autocmd-use|
  20.  
  21. {Vi does not have any of these commands}
  22.  
  23. ==============================================================================
  24. 1. Introduction                        *autocmd-intro*
  25.  
  26. You can specify commands to be executed automatically for when reading or
  27. writing a file, when entering or leaving a buffer or window, and when exiting
  28. Vim.  For example, you can create an autocommand to set the 'cindent' option
  29. for files matching *.c.  You can also use autocommands to implement advanced
  30. features, such as editing compressed files (see |gzip-example|).  The usual
  31. place to put autocommands is in your .vimrc or .exrc file.
  32.  
  33.                             *E203* *E204* *E143*
  34. WARNING: Using autocommands is very powerful, and may lead to unexpected side
  35. effects.  Be careful not to destroy your text.
  36. - It's a good idea to do some testing on an expendable copy of a file first.
  37.   For example: If you use autocommands to decompress a file when starting to
  38.   edit it, make sure that the autocommands for compressing when writing work
  39.   correctly.
  40. - Be prepared for an error halfway through (e.g., disk full).  Vim will mostly
  41.   be able to undo the changes to the buffer, but you may have to clean up the
  42.   changes to other files by hand (e.g., compress a file that has been
  43.   decompressed).
  44. - If the BufRead* events allow you to edit a compressed file, the FileRead*
  45.   events should do the same (this makes recovery possible in some rare cases).
  46.   It's a good idea to use the same autocommands for the File* and Buf* events
  47.   when possible.
  48.  
  49. The |+autocmd| feature is only included if it has not been disabled at compile
  50. time.
  51.  
  52. ==============================================================================
  53. 2. Defining autocommands                *autocmd-define*
  54.  
  55. Note: The ":autocmd" command cannot be followed by another command, since any
  56. '|' is considered part of the command.
  57.  
  58.                             *:au* *:autocmd*
  59. :au[tocmd] [group] {event} {pat} [nested] {cmd}
  60.             Add {cmd} to the list of commands that Vim will
  61.             execute automatically on {event} for a file matching
  62.             {pat}.  Vim always adds the {cmd} after existing
  63.             autocommands, so that the autocommands execute in the
  64.             order in which they were given.  See |autocmd-nested|
  65.             for [nested].
  66.  
  67. Note that special characters (e.g., "%", "<cword>") in the ":autocmd"
  68. arguments are not expanded when the autocommand is defined.  These will be
  69. expanded when the Event is recognized, and the {cmd} is executed.  The only
  70. exception is that "<sfile>" is expanded when the autocmd is defined.  Example:
  71.  >
  72.     :au BufNewFile,BufRead *.html so <sfile>:h/html.vim
  73.  
  74. Here Vim expands <sfile> to the name of the file containing this line.
  75.  
  76. When your .vimrc file is sourced twice, the autocommands will appear twice.
  77. To avoid this, put this command in your .vimrc file, before defining
  78. autocommands:
  79.  >
  80.     :autocmd!    " Remove ALL autocommands for the current group.
  81.  
  82. If you don't want to remove all autocommands, you can instead use a variable
  83. to ensure that Vim includes the autocommands only once:
  84.  >
  85.     :if !exists("autocommands_loaded")
  86.     :  let autocommands_loaded = 1
  87.     :  au ...
  88.     :endif
  89.  
  90. When the [group] argument is not given, Vim uses the current group (as defined
  91. with ":augroup"); otherwise, Vim uses the group defined with [group].  Note
  92. that [group] must have been defined before.  You cannot define a new group
  93. with ":au group ..."; use ":augroup" for that.
  94.  
  95. While testing autocommands, you might find the 'verbose' option to be useful: >
  96.     :set verbose=9
  97. This setting makes Vim echo the autocommands as it executes them.
  98.  
  99. When defining an autocommand in a script, it will be able to call functions
  100. local to the script and use mappings local to the script.  When the event is
  101. triggered and the command executed, it will run in the context of the script
  102. it was defined in.  This matters if |<SID>| is used in a command.
  103.  
  104. When executing the commands, the messages from one command overwrites a
  105. previous message.  This is different from when executing the commands
  106. manually.  Mostly the screen will not scroll up, thus there is no hit-enter
  107. prompt.  When one command outputs two messages this can happen anyway.
  108.  
  109. ==============================================================================
  110. 3. Removing autocommands                *autocmd-remove*
  111.  
  112. :au[tocmd]! [group] {event} {pat} [nested] {cmd}
  113.             Remove all autocommands associated with {event} and
  114.             {pat}, and add the command {cmd}.  See
  115.             |autocmd-nested| for [nested].
  116.  
  117. :au[tocmd]! [group] {event} {pat}
  118.             Remove all autocommands associated with {event} and
  119.             {pat}.
  120.  
  121. :au[tocmd]! [group] * {pat}
  122.             Remove all autocommands associated with {pat} for all
  123.             events.
  124.  
  125. :au[tocmd]! [group] {event}
  126.             Remove ALL autocommands for {event}.
  127.  
  128. :au[tocmd]! [group]    Remove ALL autocommands.
  129.  
  130. When the [group] argument is not given, Vim uses the current group (as defined
  131. with ":augroup"); otherwise, Vim uses the group defined with [group].
  132.  
  133. ==============================================================================
  134. 4. Listing autocommands                    *autocmd-list*
  135.  
  136. :au[tocmd] [group] {event} {pat}
  137.             Show the autocommands associated with {event} and
  138.             {pat}.
  139.  
  140. :au[tocmd] [group] * {pat}
  141.             Show the autocommands associated with {pat} for all
  142.             events.
  143.  
  144. :au[tocmd] [group] {event}
  145.             Show all autocommands for {event}.
  146.  
  147. :au[tocmd] [group]    Show all autocommands.
  148.  
  149. If you provide the [group] argument, Vim lists only the autocommands for
  150. [group]; otherwise, Vim lists the autocommands for ALL groups.  Note that this
  151. argument behavior differs from that for defining and removing autocommands.
  152.  
  153. ==============================================================================
  154. 5. Events                    *autocmd-events* *E215* *E216*
  155.  
  156.                     *autocommand-events* *{event}*
  157. Vim recognizes the following events.  Vim ignores the case of event names
  158. (e.g., you can use "BUFread" or "bufread" instead of "BufRead").
  159.  
  160.                             *BufNewFile*
  161. BufNewFile            When starting to edit a file that doesn't
  162.                 exist.  Can be used to read in a skeleton
  163.                 file.
  164.                         *BufReadPre* *E200* *E201*
  165. BufReadPre            When starting to edit a new buffer, before
  166.                 reading the file into the buffer.  Not used
  167.                 if the file doesn't exist.
  168.                         *BufRead* *BufReadPost*
  169. BufRead or BufReadPost        When starting to edit a new buffer, after
  170.                 reading the file into the buffer, before
  171.                 executing the modelines.  This does NOT work
  172.                 for ":r file".  Not used when the file doesn't
  173.                 exist.  Also used after successfully recovering
  174.                 a file.
  175.                             *BufReadCmd*
  176. BufReadCmd            Before starting to edit a new buffer.  Should
  177.                 read the file into the buffer. |Cmd-event|
  178.                             *BufFilePre*
  179. BufFilePre            Before changing the name of the current buffer
  180.                 with the ":file" command.
  181.                             *BufFilePost*
  182. BufFilePost            After changing the name of the current buffer
  183.                 with the ":file" command.
  184.                             *FileReadPre*
  185. FileReadPre            Before reading a file with a ":read" command.
  186.                             *FileReadPost*
  187. FileReadPost            After reading a file with a ":read" command.
  188.                 Note that Vim sets the '[ and '] marks to the
  189.                 first and last line of the read.  This can be
  190.                 used to operate on the lines just read.
  191.                             *FileReadCmd*
  192. FileReadCmd            Before reading a file with a ":read" command.
  193.                 Should do the reading of the file. |Cmd-event|
  194.                             *FilterReadPre* *E135*
  195. FilterReadPre            Before reading a file from a filter command.
  196.                 Vim checks the pattern against the name of
  197.                 the current buffer, not the name of the
  198.                 temporary file that is the output of the
  199.                 filter command.
  200.                             *FilterReadPost*
  201. FilterReadPost            After reading a file from a filter command.
  202.                 Vim checks the pattern against the name of
  203.                 the current buffer as with FilterReadPre.
  204.                             *FileType*
  205. FileType            When the 'filetype' option has been set.
  206.                 <afile> can be used for the name of the file
  207.                 where this option was set, and <amatch> for
  208.                 the new value of 'filetype'.
  209.                 See |filetypes|.
  210.                             *Syntax*
  211. Syntax                When the 'syntax' option has been set.
  212.                 <afile> can be used for the name of the file
  213.                 where this option was set, and <amatch> for
  214.                 the new value of 'syntax'.
  215.                 See |:syn-on|.
  216.                             *StdinReadPre*
  217. StdinReadPre            Before reading from stdin into the buffer.
  218.                 Only used when the "-" argument was used when
  219.                 Vim was started |--|.
  220.                             *StdinReadPost*
  221. StdinReadPost            After reading from the stdin into the buffer,
  222.                 before executing the modelines.  Only used
  223.                 when the "-" argument was used when Vim was
  224.                 started |--|.
  225.                         *BufWrite* *BufWritePre*
  226. BufWrite or BufWritePre        Before writing the whole buffer to a file.
  227.                             *BufWritePost*
  228. BufWritePost            After writing the whole buffer to a file
  229.                 (should undo the commands for BufWritePre).
  230.                             *BufWriteCmd*
  231. BufWriteCmd            Before writing the whole buffer to a file.
  232.                 Should do the writing of the file and reset
  233.                 'modified' if successful.  The buffer contents
  234.                 should not be changed.  |Cmd-event|
  235.                             *FileWritePre*
  236. FileWritePre            Before writing to a file, when not writing the
  237.                 whole buffer.
  238.                             *FileWritePost*
  239. FileWritePost            After writing to a file, when not writing the
  240.                 whole buffer.
  241.                             *FileWriteCmd*
  242. FileWriteCmd            Before writing to a file, when not writing the
  243.                 whole buffer.  Should do the writing to the
  244.                 file.  Should not change the buffer.
  245.                 |Cmd-event|
  246.                             *FileAppendPre*
  247. FileAppendPre            Before appending to a file.
  248.                             *FileAppendPost*
  249. FileAppendPost            After appending to a file.
  250.                             *FileAppendCmd*
  251. FileAppendCmd            Before appending to a file.  Should do the
  252.                 appending to the file. |Cmd-event|
  253.                             *FilterWritePre*
  254. FilterWritePre            Before writing a file for a filter command or
  255.                 making a diff.
  256.                 Vim checks the pattern against the name of
  257.                 the current buffer, not the name of the
  258.                 temporary file that is the output of the
  259.                 filter command.
  260.                             *FilterWritePost*
  261. FilterWritePost            After writing a file for a filter command or
  262.                 making a diff.
  263.                 Vim checks the pattern against the name of
  264.                 the current buffer as with FilterWritePre.
  265.                             *FileChangedShell*
  266. FileChangedShell        When Vim notices that the modification time of
  267.                 a file has changed since editing started.
  268.                 |timestamp|
  269.                 Mostly triggered after executing a shell
  270.                 command, but also with a |:checktime| command
  271.                 or when Vim regains input focus.
  272.                 This autocommand is triggered for each changed
  273.                 file.  It is not used when 'autoread' is set
  274.                 and the buffer was not changed.  If a
  275.                 FileChangedShell autocommand is present the
  276.                 warning message and prompt is not given.
  277.                 This is useful for reloading related buffers
  278.                 which are affected by a single command.
  279.                 NOTE: When this autocommand is executed, the
  280.                 current buffer "%" may be different from the
  281.                 buffer that was changed "<afile>".
  282.                 NOTE: The commands must not change the current
  283.                 buffer, jump to another buffer or delete a
  284.                 buffer.  *E246*
  285.                 NOTE: This event never nests, to avoid an
  286.                 endless loop.
  287.                             *FileChangedRO*
  288. FileChangedRO            Before making the first change to a read-only
  289.                 file.  Can be used to check-out the file from
  290.                 a source control system.  Not triggered when
  291.                 the change was caused by an autocommand.
  292.                 WARNING: This event is triggered when making a
  293.                 change, just before the change is applied to
  294.                 the text.  If the autocommand moves the cursor
  295.                 the effect of the change is undefined.
  296.                             *FocusGained*
  297. FocusGained            When Vim got input focus.  Only for the GUI
  298.                 version and a few console versions where this
  299.                 can be detected.
  300.                             *FocusLost*
  301. FocusLost            When Vim lost input focus.  Only for the GUI
  302.                 version and a few console versions where this
  303.                 can be detected.
  304.                             *FuncUndefined*
  305. FuncUndefined            When a user function is used but it isn't
  306.                 defined.  Useful for defining a function only
  307.                 when it's used.  Both <amatch> and <afile> are
  308.                 set to the name of the function.
  309.                             *CursorHold*
  310. CursorHold            When the user doesn't press a key for the time
  311.                 specified with 'updatetime'.  Not re-triggered
  312.                 until the user has pressed a key (i.e. doesn't
  313.                 fire every 'updatetime' ms if you leave Vim to
  314.                 make some coffee. :)  See |CursorHold-example|
  315.                 for previewing tags.
  316.                 Note: Interactive commands cannot be used for
  317.                 this event.  There is no hit-enter prompt,
  318.                 the screen is updated directly (when needed).
  319.                 Note: In the future there will probably be
  320.                 another option to set the time.
  321.                 Hint: to force an update of the status lines
  322.                 use: >
  323.                     :let &ro = &ro
  324. <                {only on Amiga, Unix, Win32, MSDOS and all GUI
  325.                 versions}
  326.                             *BufEnter*
  327. BufEnter            After entering a buffer.  Useful for setting
  328.                 options for a file type.  Also executed when
  329.                 starting to edit a buffer, after the
  330.                 BufReadPost autocommands.
  331.                             *BufLeave*
  332. BufLeave            Before leaving to another buffer.  Also when
  333.                 leaving or closing the current window and the
  334.                 new current window is not for the same buffer.
  335.                 Not used for ":qa" or ":q" when exiting Vim.
  336.                             *BufWinEnter*
  337. BufWinEnter            After a buffer is displayed in a window.  This
  338.                 can be when the buffer is loaded (after
  339.                 processing the modelines), when a hidden
  340.                 buffer is displayed in a window (and is no
  341.                 longer hidden) or a buffer already visible in
  342.                 a window is also displayed in another window.
  343.                             *BufWinLeave*
  344. BufWinLeave            Before a buffer is removed from a window.
  345.                 Not when it's still visible in another window.
  346.                 Also triggered when exiting.  It's triggered
  347.                 before BufUnload or BufHidden.
  348.                 NOTE: When this autocommand is executed, the
  349.                 current buffer "%" may be different from the
  350.                 buffer being unloaded "<afile>".
  351.                             *BufUnload*
  352. BufUnload            Before unloading a buffer.  This is when the
  353.                 text in the buffer is going to be freed.  This
  354.                 may be after a BufWritePost and before a
  355.                 BufDelete.  Also used for all buffers that are
  356.                 loaded when Vim is going to exit.
  357.                 NOTE: When this autocommand is executed, the
  358.                 current buffer "%" may be different from the
  359.                 buffer being unloaded "<afile>".
  360.                             *BufHidden*
  361. BufHidden            Just after a buffer has become hidden.  That
  362.                 is, when there are no longer windows that show
  363.                 the buffer, but the buffer is not unloaded or
  364.                 deleted.  Not used for ":qa" or ":q" when
  365.                 exiting Vim.
  366.                 NOTE: When this autocommand is executed, the
  367.                 current buffer "%" may be different from the
  368.                 buffer being unloaded "<afile>".
  369.                             *BufNew*
  370. BufNew                Just after creating a new buffer.  Also used
  371.                 just after a buffer has been renamed.  When
  372.                 the buffer is added to the buffer list BufAdd
  373.                 will be triggered too.
  374.                 NOTE: When this autocommand is executed, the
  375.                 current buffer "%" may be different from the
  376.                 buffer being created "<afile>".
  377.                             *BufCreate* *BufAdd*
  378. BufAdd or BufCreate        Just after creating a new buffer which is
  379.                 added to the buffer list, or adding a buffer
  380.                 to the buffer list.
  381.                 Also used just after a buffer in the buffer
  382.                 list has been renamed.
  383.                 The BufCreate event is for historic reasons.
  384.                 NOTE: When this autocommand is executed, the
  385.                 current buffer "%" may be different from the
  386.                 buffer being created "<afile>".
  387.                             *BufDelete*
  388. BufDelete            Before deleting a buffer from the buffer list.
  389.                 The BufUnload may be called first (if the
  390.                 buffer was loaded).
  391.                 Also used just before a buffer in the buffer
  392.                 list is renamed.
  393.                 NOTE: When this autocommand is executed, the
  394.                 current buffer "%" may be different from the
  395.                 buffer being deleted "<afile>".
  396.                             *BufWipeout*
  397. BufWipeout            Before completely deleting a buffer.  The
  398.                 BufUnload and BufDelete events may be called
  399.                 first (if the buffer was loaded and was in the
  400.                 buffer list).  Also used just before a buffer
  401.                 is renamed (also when it's not in the buffer
  402.                 list).
  403.                 NOTE: When this autocommand is executed, the
  404.                 current buffer "%" may be different from the
  405.                 buffer being deleted "<afile>".
  406.                             *WinEnter*
  407. WinEnter            After entering another window.  Not done for
  408.                 the first window, when Vim has just started.
  409.                 Useful for setting the window height.
  410.                 If the window is for another buffer, Vim
  411.                 executes the BufEnter autocommands after the
  412.                 WinEnter autocommands.
  413.                             *WinLeave*
  414. WinLeave            Before leaving a window.  If the window to be
  415.                 entered next is for a different buffer, Vim
  416.                 executes the BufLeave autocommands before the
  417.                 WinLeave autocommands (but not for ":new").
  418.                 Not used for ":qa" or ":q" when exiting Vim.
  419.                             *CmdwinEnter*
  420. CmdwinEnter            After entering the command-line window.
  421.                 Useful for setting options specifically for
  422.                 this special type of window.  This is
  423.                 triggered _instead_ of BufEnter and WinEnter.
  424.                 <afile> is set to a single character,
  425.                 indicating the type of command-line.
  426.                 |cmdwin-char|
  427.                             *CmdwinLeave*
  428. CmdwinLeave            Before leaving the command-line window.
  429.                 Useful to clean up any global setting done
  430.                 with CmdwinEnter.  This is triggered _instead_
  431.                 of BufLeave and WinLeave.
  432.                 <afile> is set to a single character,
  433.                 indicating the type of command-line.
  434.                 |cmdwin-char|
  435.                             *GUIEnter*
  436. GUIEnter            After starting the GUI successfully, and after
  437.                 opening the window.  It is triggered before
  438.                 VimEnter when using gvim.  Can be used to
  439.                 position the window from a .gvimrc file: >
  440.     :autocmd GUIEnter * winpos 100 50
  441. <                            *VimEnter*
  442. VimEnter            After doing all the startup stuff, including
  443.                 loading .vimrc files, executing the "-c cmd"
  444.                 arguments, creating all windows and loading
  445.                 the buffers in them.
  446.                             *VimLeavePre*
  447. VimLeavePre            Before exiting Vim, just before writing the
  448.                 .viminfo file.  This is executed only once,
  449.                 if there is a match with the name of what
  450.                 happens to be the current buffer when exiting.
  451.                 Mostly useful with a "*" pattern. >
  452.     :autocmd VimLeavePre * call CleanupStuff()
  453. <                            *VimLeave*
  454. VimLeave            Before exiting Vim, just after writing the
  455.                 .viminfo file.  Executed only once, like
  456.                 VimLeavePre.
  457.                             *EncodingChanged*
  458. EncodingChanged            Fires off when the 'encoding' option is
  459.                 changed.  Useful to set up fonts, for example.
  460.                             *FileEncoding*
  461. FileEncoding            Obsolete.  It still works and is equivalent
  462.                 to |EncodingChanged|.
  463.                             *RemoteReply*
  464. RemoteReply            When a reply from a Vim that functions as
  465.                 server was received |server2client()|.
  466.                 <amatch> is equal to the {serverid} from which
  467.                 the reply was sent, and <afile> is the actual
  468.                 reply string.
  469.                 Note that even if an auto command is defined,
  470.                 the reply should be read with |remote_read()|
  471.                 to consume it.
  472.                             *TermChanged*
  473. TermChanged            After the value of 'term' has changed.  Useful
  474.                 for re-loading the syntax file to update the
  475.                 colors, fonts and other terminal-dependent
  476.                 settings.  Executed for all loaded buffers.
  477.                             *TermResponse*
  478. TermResponse            After the response to |t_RV| is received from
  479.                 the terminal.  The value of |v:termresponse|
  480.                 can be used to do things depending on the
  481.                 terminal version.
  482.                             *UserGettingBored*
  483. UserGettingBored        When the user hits CTRL-C.  Just kidding! :-)
  484.                             *User*
  485. User                Never executed automatically.  To be used for
  486.                 autocommands that are only executed with
  487.                 ":doautocmd".
  488.  
  489.  
  490. For READING FILES there are three possible pairs of events.  Vim uses only one
  491. pair at a time:
  492. BufNewFile            starting to edit a non-existent file
  493. BufReadPre    BufReadPost    starting to edit an existing file
  494. FilterReadPre    FilterReadPost    read the temp file with filter output
  495. FileReadPre    FileReadPost    any other file read
  496.  
  497. Note that the autocommands for the *ReadPre events and all the Filter events
  498. are not allowed to change the current buffer (you will get an error message if
  499. this happens).  This is to prevent the file to be read into the wrong buffer.
  500.  
  501. Note that the 'modified' flag is reset AFTER executing the BufReadPost
  502. and BufNewFile autocommands.  But when the 'modified' option was set by the
  503. autocommands, this doesn't happen.
  504.  
  505. You can use the 'eventignore' option to ignore a number of events or all
  506. events.
  507.  
  508. ==============================================================================
  509. 6. Patterns                        *autocmd-patterns*
  510.  
  511. The file pattern {pat} is tested for a match against the file name in one of
  512. two ways:
  513. 1. When there is no '/' in the pattern, Vim checks for a match against only
  514.    the tail part of the file name (without its leading directory path).
  515. 2. When there is a '/' in the pattern,  Vim checks for a match against the
  516.    both short file name (as you typed it) and the full file name (after
  517.    expanding it to a full path and resolving symbolic links).
  518.  
  519. Examples: >
  520.     :autocmd BufRead *.txt        set et
  521. Set the 'et' option for all text files. >
  522.  
  523.     :autocmd BufRead /vim/src/*.c    set cindent
  524. Set the 'cindent' option for C files in the /vim/src directory. >
  525.  
  526.     :autocmd BufRead /tmp/*.c    set ts=5
  527. If you have a link from "/tmp/test.c" to "/home/nobody/vim/src/test.c", and
  528. you start editing "/tmp/test.c", this autocommand will match.
  529.  
  530. Note:  To match part of a path, but not from the root directory, use a '*' as
  531. the first character.  Example: >
  532.     :autocmd BufRead */doc/*.txt    set tw=78
  533. This autocommand will for example be executed for "/tmp/doc/xx.txt" and
  534. "/usr/home/piet/doc/yy.txt".  The number of directories does not matter here.
  535.  
  536.  
  537. The file name that the pattern is matched against is after expanding
  538. wildcards.  Thus is you issue this command: >
  539.     :e $ROOTDIR/main.$EXT
  540. The argument is first expanded to: >
  541.     /usr/root/main.py
  542. Before it's matched with the pattern of the autocommand.  Careful with this
  543. when using events like FileReadCmd, the value of <amatch> may not be what you
  544. expect.
  545.  
  546.  
  547. Environment variables can be used in a pattern: >
  548.     :autocmd BufRead $VIMRUNTIME/doc/*.txt  set expandtab
  549. And ~ can be used for the home directory (if $HOME is defined): >
  550.     :autocmd BufWritePost ~/.vimrc   so ~/.vimrc
  551.     :autocmd BufRead ~archive/*      set readonly
  552. The environment variable is expanded when the autocommand is defined, not when
  553. the autocommand is executed.  This is different from the command!
  554.  
  555.                             *file-pattern*
  556. The pattern is interpreted like mostly used in file names:
  557.     *    matches any sequence of characters
  558.     ?    matches any single character
  559.     \?    matches a '?'
  560.     .    matches a '.'
  561.     ~    matches a '~'
  562.     ,    separates patterns
  563.     \,    matches a ','
  564.     { }    like \( \) in a |pattern|
  565.     ,    inside { }: like \| in a |pattern|
  566.     \    special meaning like in a |pattern|
  567.     [ch]    matches 'c' or 'h'
  568.  
  569. Note that for all systems the '/' character is used for path separator (even
  570. MS-DOS and OS/2).  This was done because the backslash is difficult to use
  571. in a pattern and to make the autocommands portable across different systems.
  572.  
  573.  
  574. Matching with the pattern is done when an event is triggered.  Changing the
  575. buffer name in one of the autocommands, or even deleting the buffer, does not
  576. change which autocommands will be executed.  Example: >
  577.  
  578.     au BufEnter *.foo  bdel
  579.     au BufEnter *.foo  set modified
  580.  
  581. This will delete the current buffer and then set 'modified' in what has become
  582. the current buffer instead.  Vim doesn't take into account that "*.foo"
  583. doesn't match with that buffer name.  It matches "*.foo" with the name of the
  584. buffer at the moment the event was triggered.
  585.  
  586. ==============================================================================
  587. 7. Groups                        *autocmd-groups*
  588.  
  589. Autocommands can be put together in a group.  This is useful for removing or
  590. executing a group of autocommands.  For example, all the autocommands for
  591. syntax highlighting are put in the "highlight" group, to be able to execute
  592. ":doautoall highlight BufRead" when the GUI starts.
  593.  
  594. When no specific group is selected, Vim uses the default group.  The default
  595. group does not have a name.  You cannot execute the autocommands from the
  596. default group separately; you can execute them only by executing autocommands
  597. for all groups.
  598.  
  599. Normally, when executing autocommands automatically, Vim uses the autocommands
  600. for all groups.  The group only matters when executing autocommands with
  601. ":doautocmd" or ":doautoall", or when defining or deleting autocommands.
  602.  
  603. The group name can contain any characters except white space.  The group name
  604. "end" is reserved (also in uppercase).  The group name is case sensitive.
  605.  
  606.                             *:aug* *:augroup*
  607. :aug[roup] {name}        Define the autocmd group name for the
  608.                 following ":autocmd" commands.  The name "end"
  609.                 or "END" selects the default group.
  610.  
  611.                         *:augroup-delete* *E367*
  612. :aug[roup]! {name}        Delete the autocmd group {name}.  Don't use
  613.                 this if there is still an autocommand using
  614.                 this group!  This is not checked.
  615.  
  616. To enter autocommands for a specific group, use this method:
  617. 1. Select the group with ":augroup {name}".
  618. 2. Delete any old autocommands with ":au!".
  619. 3. Define the autocommands.
  620. 4. Go back to the default group with "augroup END".
  621.  
  622. Example: >
  623.     :augroup uncompress
  624.     :  au!
  625.     :  au BufEnter *.gz    %!gunzip
  626.     :augroup END
  627.  
  628. This prevents having the autocommands defined twice (e.g., after sourcing the
  629. .vimrc file again).
  630.  
  631. ==============================================================================
  632. 8. Executing autocommands                *autocmd-execute*
  633.  
  634. Vim can also execute Autocommands non-automatically.  This is useful if you
  635. have changed autocommands, or when Vim has executed the wrong autocommands
  636. (e.g., the file pattern match was wrong).
  637.  
  638. Note that the 'eventignore' option applies here too.  Events listed in this
  639. option will not cause any commands to be executed.
  640.  
  641.                         *:do* *:doautocmd* *E217*
  642. :do[autocmd] [group] {event} [fname]
  643.             Apply the autocommands matching [fname] (default:
  644.             current file name) for {event} to the current buffer.
  645.             You can use this when the current file name does not
  646.             match the right pattern, after changing settings, or
  647.             to execute autocommands for a certain event.
  648.             It's possible to use this inside an autocommand too,
  649.             so you can base the autocommands for one extension on
  650.             another extension.  Example: >
  651.                 :au Bufenter *.cpp so ~/.vimrc_cpp
  652.                 :au Bufenter *.cpp doau BufEnter x.c
  653. <            Be careful to avoid endless loops.  See
  654.             |autocmd-nested|.
  655.  
  656.             When the [group] argument is not given, Vim executes
  657.             the autocommands for all groups.  When the [group]
  658.             argument is included, Vim executes only the matching
  659.             autocommands for that group.  Note: if you use an
  660.             undefined group name, Vim gives you an error message.
  661.  
  662.                         *:doautoa* *:doautoall*
  663. :doautoa[ll] [group] {event} [fname]
  664.             Like ":doautocmd", but apply the autocommands to each
  665.             loaded buffer.  Careful: Don't use this for
  666.             autocommands that delete a buffer, change to another
  667.             buffer or change the contents of a buffer; the result
  668.             is unpredictable.  This command is intended for
  669.             autocommands that set options, change highlighting,
  670.             and things like that.
  671.  
  672. ==============================================================================
  673. 9. Using autocommands                    *autocmd-use*
  674.  
  675. For WRITING FILES there are four possible sets of events.  Vim uses only one
  676. of these sets for a write command:
  677.  
  678. BufWriteCmd    BufWritePre    BufWritePost    writing the whole buffer
  679.         FilterWritePre    FilterWritePost    writing to filter temp file
  680. FileAppendCmd    FileAppendPre    FileAppendPost    appending to a file
  681. FileWriteCmd    FileWritePre    FileWritePost    any other file write
  682.  
  683. When there is a matching "*Cmd" autocommand, it is assumed it will do the
  684. writing.  No further writing is done and the other events are not triggered.
  685. |Cmd-event|
  686.  
  687. Note that the *WritePost commands should undo any changes to the buffer that
  688. were caused by the *WritePre commands; otherwise, writing the file will have
  689. the side effect of changing the buffer.
  690.  
  691. Before executing the autocommands, the buffer from which the lines are to be
  692. written temporarily becomes the current buffer.  Unless the autocommands
  693. change the current buffer or delete the previously current buffer, the
  694. previously current buffer is made the current buffer again.
  695.  
  696. The *WritePre and *AppendPre autocommands must not delete the buffer from
  697. which the lines are to be written.
  698.  
  699. The '[ and '] marks have a special position:
  700. - Before the *ReadPre event the '[ mark is set to the line just above where
  701.   the new lines will be inserted.
  702. - Before the *ReadPost event the '[ mark is set to the first line that was
  703.   just read, the '] mark to the last line.
  704. - Before executing the *WritePre and *AppendPre autocommands the '[ mark is
  705.   set to the first line that will be written, the '] mark to the last line.
  706. Careful: '[ and '] change when using commands that change the buffer.
  707.  
  708. In commands which expect a file name, you can use "<afile>" for the file name
  709. that is being read |:<afile>| (you can also use "%" for the current file
  710. name).  "<abuf>" can be used for the buffer number of the currently effective
  711. buffer.  This also works for buffers that doesn't have a name.  But it doesn't
  712. work for files without a buffer (e.g., with ":r file").
  713.  
  714.                             *gzip-example*
  715. Examples for reading and writing compressed files: >
  716.   :augroup gzip
  717.   :  autocmd!
  718.   :  autocmd BufReadPre,FileReadPre    *.gz set bin
  719.   :  autocmd BufReadPost,FileReadPost    *.gz '[,']!gunzip
  720.   :  autocmd BufReadPost,FileReadPost    *.gz set nobin
  721.   :  autocmd BufReadPost,FileReadPost    *.gz execute ":doautocmd BufReadPost " . expand("%:r")
  722.   :  autocmd BufWritePost,FileWritePost    *.gz !mv <afile> <afile>:r
  723.   :  autocmd BufWritePost,FileWritePost    *.gz !gzip <afile>:r
  724.  
  725.   :  autocmd FileAppendPre        *.gz !gunzip <afile>
  726.   :  autocmd FileAppendPre        *.gz !mv <afile>:r <afile>
  727.   :  autocmd FileAppendPost        *.gz !mv <afile> <afile>:r
  728.   :  autocmd FileAppendPost        *.gz !gzip <afile>:r
  729.   :augroup END
  730.  
  731. The "gzip" group is used to be able to delete any existing autocommands with
  732. ":autocmd!", for when the file is sourced twice.
  733.  
  734. ("<afile>:r" is the file name without the extension, see |:_%:|)
  735.  
  736. The commands executed for the BufNewFile, BufRead/BufReadPost, BufWritePost,
  737. FileAppendPost and VimLeave events do not set or reset the changed flag of the
  738. buffer.  When you decompress the buffer with the BufReadPost autocommands, you
  739. can still exit with ":q".  When you use ":undo" in BufWritePost to undo the
  740. changes made by BufWritePre commands, you can still do ":q" (this also makes
  741. "ZZ" work).  If you do want the buffer to be marked as modified, set the
  742. 'modified' option.
  743.  
  744. To execute Normal mode commands from an autocommand, use the ":normal"
  745. command.  Use with care!  If the Normal mode command is not finished, the user
  746. needs to type characters (e.g., after ":normal m" you need to type a mark
  747. name).
  748.  
  749. If you want the buffer to be unmodified after changing it, reset the
  750. 'modified' option.  This makes it possible to exit the buffer with ":q"
  751. instead of ":q!".
  752.  
  753.                             *autocmd-nested* *E218*
  754. By default, autocommands do not nest.  If you use ":e" or ":w" in an
  755. autocommand, Vim does not execute the BufRead and BufWrite autocommands for
  756. those commands.  If you do want this, use the "nested" flag for those commands
  757. in which you want nesting.  For example: >
  758.   :autocmd FileChangedShell *.c nested e!
  759. The nesting is limited to 10 levels to get out of recursive loops.
  760.  
  761. It's possible to use the ":au" command in an autocommand.  This can be a
  762. self-modifying command!  This can be useful for an autocommand that should
  763. execute only once.
  764.  
  765. There is currently no way to disable the autocommands.  If you want to write a
  766. file without executing the autocommands for that type of file, write it under
  767. another name and rename it with a shell command.
  768.  
  769. Note: When reading a file (with ":read file" or with a filter command) and the
  770. last line in the file does not have an <EOL>, Vim remembers this.  At the next
  771. write (with ":write file" or with a filter command), if the same line is
  772. written again as the last line in a file AND 'binary' is set, Vim does not
  773. supply an <EOL>.  This makes a filter command on the just read lines write the
  774. same file as was read, and makes a write command on just filtered lines write
  775. the same file as was read from the filter.  For example, another way to write
  776. a compressed file: >
  777.  
  778.   :autocmd FileWritePre *.gz   set bin|'[,']!gzip
  779.   :autocmd FileWritePost *.gz  undo|set nobin
  780. <
  781.                             *autocommand-pattern*
  782. You can specify multiple patterns, separated by commas.  Here are some
  783. examples: >
  784.  
  785.   :autocmd BufRead   *        set tw=79 nocin ic infercase fo=2croq
  786.   :autocmd BufRead   .letter    set tw=72 fo=2tcrq
  787.   :autocmd BufEnter  .letter    set dict=/usr/lib/dict/words
  788.   :autocmd BufLeave  .letter    set dict=
  789.   :autocmd BufRead,BufNewFile   *.c,*.h    set tw=0 cin noic
  790.   :autocmd BufEnter  *.c,*.h    abbr FOR for (i = 0; i < 3; ++i)<CR>{<CR>}<Esc>O
  791.   :autocmd BufLeave  *.c,*.h    unabbr FOR
  792.  
  793. For makefiles (makefile, Makefile, imakefile, makefile.unix, etc.): >
  794.  
  795.   :autocmd BufEnter  ?akefile*    set include=^s\=include
  796.   :autocmd BufLeave  ?akefile*    set include&
  797.  
  798. To always start editing C files at the first function: >
  799.  
  800.   :autocmd BufRead   *.c,*.h    1;/^{
  801.  
  802. Without the "1;" above, the search would start from wherever the file was
  803. entered, rather than from the start of the file.
  804.  
  805.                         *skeleton* *template*
  806. To read a skeleton (template) file when opening a new file: >
  807.  
  808.   :autocmd BufNewFile  *.c    0r ~/vim/skeleton.c
  809.   :autocmd BufNewFile  *.h    0r ~/vim/skeleton.h
  810.   :autocmd BufNewFile  *.java    0r ~/vim/skeleton.java
  811.  
  812. To insert the current date and time in a *.html file when writing it: >
  813.  
  814.   :autocmd BufWritePre,FileWritePre *.html   ks|call LastMod()|'s
  815.   :fun LastMod()
  816.   :  if line("$") > 20
  817.   :    let l = 20
  818.   :  else
  819.   :    let l = line("$")
  820.   :  endif
  821.   :  exe "1," . l . "g/Last modified: /s/Last modified: .*/Last modified: " .
  822.   :  \ strftime("%Y %b %d")
  823.   :endfun
  824.  
  825. You need to have a line "Last modified: <date time>" in the first 20 lines
  826. of the file for this to work.  Vim replaces <date time> (and anything in the
  827. same line after it) with the current date and time.  Explanation:
  828.     ks        mark current position with mark 's'
  829.     call LastMod()  call the LastMod() function to do the work
  830.     's        return the cursor to the old position
  831. The LastMod() function checks if the file is shorter than 20 lines, and then
  832. uses the ":g" command to find lines that contain "Last modified: ".  For those
  833. lines the ":s" command is executed to replace the existing date with the
  834. current one.  The ":execute" command is used to be able to use an expression
  835. for the ":g" and ":s" commands.  The date is obtained with the strftime()
  836. function.  You can change its argument to get another date string.
  837.  
  838. When entering :autocmd on the command-line, completion of events and command
  839. names may be done (with <Tab>, CTRL-D, etc.) where appropriate.
  840.  
  841. Vim executes all matching autocommands in the order that you specify them.
  842. It is recommended that your first autocommand be used for all files by using
  843. "*" as the file pattern.  This means that you can define defaults you like
  844. here for any settings, and if there is another matching autocommand it will
  845. override these.  But if there is no other matching autocommand, then at least
  846. your default settings are recovered (if entering this file from another for
  847. which autocommands did match).  Note that "*" will also match files starting
  848. with ".", unlike Unix shells.
  849.  
  850.                             *autocmd-searchpat*
  851. Autocommands do not change the current search patterns.  Vim saves the current
  852. search patterns before executing autocommands then restores them after the
  853. autocommands finish.  This means that autocommands do not affect the strings
  854. highlighted with the 'hlsearch' option.  Within autocommands, you can still
  855. use search patterns normally, e.g., with the "n" command.
  856. If you want an autocommand to set the search pattern, such that it is used
  857. after the autocommand finishes, use the ":let @/ =" command.
  858. The search-highlighting cannot be switched off with ":nohlsearch" in an
  859. autocommand.  Use the 'h' flag in the 'viminfo' option to disable search-
  860. highlighting when starting Vim.
  861.  
  862.                             *Cmd-event*
  863. When using one of the "*Cmd" events, the matching autocommands are expected to
  864. do the file reading or writing.  This can be used when working with a special
  865. kind of file, for example on a remote system.
  866. CAREFUL: If you use these events in a wrong way, it may have the effect of
  867. making it impossible to read or write the matching files!  Make sure you test
  868. your autocommands properly.  Best is to use a pattern that will never match a
  869. normal file name, for example "ftp://*".
  870.  
  871. When defining a BufReadCmd it will be difficult for Vim to recover a crashed
  872. editing session.  When recovering from the original file, Vim reads only those
  873. parts of a file that are not found in the swap file.  Since that is not
  874. possible with a BufReadCmd, use the |:preserve| command to make sure the
  875. original file isn't needed for recovery.  You might want to do this only when
  876. you expect the file to be modified.
  877.  
  878. The |v:cmdarg| variable holds the "++enc=" and "++ff=" argument that are
  879. effective.  These should be used for the command that reads/writes the file.
  880.  
  881. See the $VIMRUNTIME/plugin/netrw.vim for examples.
  882.  
  883.  vim:tw=78:ts=8:ft=help:norl:
  884.